package mdrtu;

import java.util.Arrays;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersResponse;

public class reinput_3 {
	public static String commPortId;
	public static int baudRate; 
	public static int dataBits;
	public static int stopBits;
	public static int parity;
	public static int slaveId;
	public static int start;
	public static int len;
	public reinput_3(String commPortId1, int baudRate1, int dataBits1, int stopBits1, int parity1, int slaveId1, int start1, int len1)
	{
		 commPortId = commPortId1; 
		 baudRate = baudRate1; 
		 dataBits = dataBits1;
		 stopBits = stopBits1;
		 parity = parity1;
		 slaveId = slaveId1;
		 start = start1;
		 len  = len1;
	};
	public static int[] remain1(){
		short result[] = null;
		SerialParameters serialParameters = serialParaSet(commPortId, baudRate, dataBits,  stopBits,  parity);
		ModbusFactory modbusFactory = new ModbusFactory();

        ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        result = readInputRegistersTest(master, slaveId, start, len);
        int resultint[] = new int[result.length];
        for(int i=0; i<result.length; i++)
           {//System.out.println(result[i]);
     	  
     	   resultint[i] =(int)result[i]; 
     	  // System.out.println("1111");
     	   
     	   System.out.println(resultint[i]);
     	   }
        master.destroy();
        return resultint;
	};
	private static SerialParameters serialParaSet(String commPortId3, int baudRate3, int dataBits3, int stopBits3, int parity3) {
		SerialParameters serialParameters = new SerialParameters();		
		serialParameters.setCommPortId(commPortId3);//璁惧畾MODBUS閫氳鐨勪覆琛屽彛		
		serialParameters.setParity(parity3);//璁惧畾鎴愭棤濂囧伓鏍￠獙		
		serialParameters.setDataBits(dataBits3);//璁惧畾鎴愭暟鎹綅鏄�8浣�		
		serialParameters.setStopBits(stopBits3);//璁惧畾涓�1涓仠姝綅
		serialParameters.setPortOwnerName("Numb nuts");		
		serialParameters.setBaudRate(baudRate3);//涓茶鍙ｄ笂鐨勬尝鐗圭巼
		return serialParameters;
	}
	public static short[] readInputRegistersTest(ModbusMaster master, int slaveId3, int start3, int len3) {
		short[] result1 = null;
		try {
            ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId3, start3, len3);
            ReadInputRegistersResponse response = (ReadInputRegistersResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
                System.out.println(Arrays.toString(response.getShortData()));
            result1 = response.getShortData();
		}
        catch (ModbusTransportException e) {
            e.printStackTrace();
        }
		return result1;
    }


}
